package com.gopay.checkorder.service.task.thread;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.manager.BankOrderManager;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;

/**
 * 
 * @author lizheng
 *
 */
public class UnAcctCompensateThread implements Runnable {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    
    
    private BankOrderManager bankOrderManager;
    private ConcurrentLinkedQueue<BankOrder> queue;
    private CountDownLatch latch;
    

    public UnAcctCompensateThread(BankOrderManager bankOrderManager, ConcurrentLinkedQueue<BankOrder> queue,
            CountDownLatch latch) {
        this.bankOrderManager = bankOrderManager;
        this.queue = queue;
        this.latch = latch;
    }

    @Override
    public void run() {
        BankOrder bankOrder = null;
        while ((bankOrder = queue.poll()) != null) {
            try {
                
            	CheckOrderResult result = buildOrderResult(bankOrder);
            	logger.error("UnAcctCompensate result info:"+result.toString());
            	bankOrderManager.inAcct(result);
                logger.info("**********************当前队列剩余订单数：" + queue.size());
            } catch (Exception e) {
                logger.info("---------- 对单error---------", e);
            } finally {
                latch.countDown();
            }
        }
    }
    private CheckOrderResult buildOrderResult(BankOrder bankOrder){
    	CheckOrderResult result = new CheckOrderResult();
        InAccInfo accInfo = new InAccInfo();
        result.setAccInfo(accInfo);
        accInfo.setOrderId(bankOrder.getOrderId());
        accInfo.setRetCode("00");
        accInfo.setCardType(BankCardType.Unknown.toString());
        accInfo.setAmount(bankOrder.getSendBankAmt());
		return result;
    	
    }

}
